package com.itheima.web;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.Orders;

import com.itheima.domain.Traveller;
import com.itheima.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping(path = "/orders")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @RequestMapping(path = "/findAll")
    public String findAll(Model model, @RequestParam(name = "page", required = true, defaultValue = "1") int page, @RequestParam(name = "pageSize", required = true, defaultValue = "4") int pageSize) {
        List<Orders> list = ordersService.findAllOrders(page, pageSize);
        for (Orders orders : list) {
            System.out.println(orders.getOrderTime() + "===");
        }
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("ordersList", pageInfo);
        return "orders-list";
    }

    /**
     * 通过id获取订单详情
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(path = "/findById")
    public String findById(Model model, @RequestParam(name = "id", required = true) String id) {
        Orders orders = ordersService.findById(id);
        model.addAttribute("orders", orders);
        return "orders-show";
    }

//    /**
//     * 通过id获取订单详情
//     * @param id
//     * @return
//     */
//    @RequestMapping(path = "/findById")
//    public ModelAndView findById(@RequestParam(name = "id", required = true) String id) {
//        Orders order = ordersService.findById(id);
//        ModelAndView mv = new ModelAndView();
//        mv.addObject("orders", order);
//        mv.setViewName("orders-show");
//        return mv;
//    }
}
